public class Demo39 {
    private static int sum = 0;

    public static void main(String[] args) throws InterruptedException {
        // 创建一个线程, 让这个线程来实现 1 + 2 + 3 + ... + 1000
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                int result = 0;
                for (int i = 1; i <= 1000; i++) {
                    result += i;
                }
                // 此处为了把 result 告知主线程, 就需要通过成员变量倒腾一下.
                sum = result;
            }
        });
        t.start();
        t.join();

        // 主线程获取到结果.
        System.out.println(sum);
    }
}